package edu.upc.clases.clase5.dao;

import edu.upc.clases.clase5.entity.Local;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gian
 */
public class LocalDAO extends BaseDAO {

    public Integer insertar(Local local) {
        String insert = "INSERT INTO LOCAL (DESCRIPCIONLOCAL,CODIGOUBIGEO,CALLE,NRO,URBANIZACION,COORDENADAGOOGLEMAPS,TELEFONO,CODIGOCANCHA) VALUES(?,?,?,?,?,?,?,?)";
        List list = new ArrayList();
        // list.add(persona.getCodigoPersona());
        list.add(local.getDescripcionLocal());
        list.add(local.getCodigoUbigeo());
        list.add(local.getCalle());
        list.add(local.getNro());
        list.add(local.getUrbanizacion());
        list.add(local.getCoordenadaGoogleMaps());
        list.add(local.getTelefono());
        list.add(local.getCodigoCancha());
        return this.insert(insert, list);
    }

    public void actualizar(Local local) {
        String update = "UPDATE LOCAL SET DESCRIPCIONLOCAL=?,CODIGOUBIGEO=?,CALLE=?,NRO=?,URBANIZACION=?,COORDENADAGOOGLEMAPS=?,TELEFONO=?,CODIGOCANCHA=? WHERE CODIGOLOCAL=?";
        List list = new ArrayList();
        list.add(local.getDescripcionLocal());
        list.add(local.getCodigoUbigeo());
        list.add(local.getCalle());
        list.add(local.getNro());
        list.add(local.getUrbanizacion());
        list.add(local.getCoordenadaGoogleMaps());
        list.add(local.getTelefono());
        list.add(local.getCodigoCancha());
        list.add(local.getCodigoLocal());
        this.update(update, list);
    }

    public void eliminar(Local local) {
        String delete = "delete from LOCAL where CODIGOLOCAL = ?";
        this.delete(delete, local.getCodigoLocal());
    }

    public List<Local> buscar() {
        ResultSet rs = this.select("select CODIGOLOCAL,DESCRIPCIONLOCAL,CODIGOUBIGEO,CALLE,NRO,URBANIZACION,COORDENADAGOOGLEMAPS,TELEFONO,CODIGOCANCHA from LOCAL");
        List<Local> listado = new ArrayList<Local>();
        try {
            while (rs.next()) {
                Local local = new Local();

                local.setCodigoLocal(rs.getInt(1));
                local.setDescripcionLocal(rs.getString(2));
                local.setCodigoUbigeo(rs.getString(3));
                local.setCalle(rs.getString(4));
                local.setNro(rs.getInt(5));
                local.setUrbanizacion(rs.getString(6));
                local.setCoordenadaGoogleMaps(rs.getString(7));
                local.setTelefono(rs.getString(8));
                local.setCodigoCancha(rs.getString(9));

                listado.add(local);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LocalDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listado;
    }

        public List<Local> buscarLocalNombre(String pNombre) {
        ResultSet rs = this.select("select CODIGOLOCAL,DESCRIPCIONLOCAL,CODIGOUBIGEO,CALLE,NRO,URBANIZACION,COORDENADAGOOGLEMAPS,TELEFONO,CODIGOCANCHA from LOCAL");
        List<Local> listado = new ArrayList<Local>();
        try {
            while (rs.next()) {
                Local local = new Local();

                local.setCodigoLocal(rs.getInt(1));
                local.setDescripcionLocal(rs.getString(2));
                local.setCodigoUbigeo(rs.getString(3));
                local.setCalle(rs.getString(4));
                local.setNro(rs.getInt(5));
                local.setUrbanizacion(rs.getString(6));
                local.setCoordenadaGoogleMaps(rs.getString(7));
                local.setTelefono(rs.getString(8));
                local.setCodigoCancha(rs.getString(9));

                listado.add(local);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LocalDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listado;
    }
}
